package com.fudan.backend.service.impl;

import com.fudan.backend.dao.CommodityDao;
import com.fudan.backend.dao.DeliveryInfoDao;
import com.fudan.backend.dao.OrderDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.*;
import com.fudan.backend.pojo.statics.DeliveryStatus;
import com.fudan.backend.pojo.statics.OrderStatus;
import com.fudan.backend.service.DeliveryService;
import com.fudan.backend.service.StoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeliveryServiceImpl implements DeliveryService {
    @Autowired
    DeliveryInfoDao deliveryInfoDao;
    @Autowired
    StoreService storeService;
    @Autowired
    OrderDao orderDao;
    @Autowired
    CommodityDao commodityDao;

    @Override
    public PageInfo<DeliveryInfo> getDeliveryInfoByStoreId(String storeId, String userId, Integer current, Integer size) {
        storeService.checkStoreAuthorize(storeId, userId);
        PageHelper.startPage(current, size);
        List<DeliveryInfo> list = deliveryInfoDao.getDeliveryInfoByStoreId(storeId);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public RetCode setDeliveryInfoShip(String deliveryInfoId, String userId) {
        DeliveryInfo dInfo = deliveryInfoDao.getDeliveryInfoByDeliveryInfoId(deliveryInfoId);
        if (dInfo == null) {
            throw new BaseException(RetCode.DELIVERY_INFO_NOT_FOUND);
        }
        Store store = storeService.getStoreByStoreId(dInfo.getStoreId());
        if (!store.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        Date current = new Date(System.currentTimeMillis());
        int status = dInfo.getStatus();
        if (status != DeliveryStatus.NOT_SHIPPED) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        Order order = dInfo.getOrder();
        //商品库存更新
        for(OrderItem item : order.getOrderItemList()){
            Commodity commodity = item.getCommodity();
            int stocks = commodity.getStocks();
            int saleVolume = commodity.getSalesVolume();
            if(stocks < item.getCommodityNum()){
                throw new BaseException(RetCode.COMMODITY_STOCKS_INSUFFICIENT);
            }
            //库存减少
            commodityDao.addCommodityStock(commodity.getCommodityId(), -item.getCommodityNum());

        }
        order.setDeliveryTime(current);
        order.setStatus(OrderStatus.SHIPPED_AND_NOT_RECEIVED);
        orderDao.updateOrder(order);
        deliveryInfoDao.setDeliveryInfoStatus(dInfo.getDeliveryInfoId(), DeliveryStatus.SHIPPED);
        return RetCode.DELIVERY_INFO_SUCCESS;
    }
}
